package logic

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"order_srv/internal/dao"
	"order_srv/internal/svc"
	"order_srv/order"
	"smart-health-platforms/pkg"
	"smart-health-platforms/untils"
)

type OrderCreateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderCreateLogic {
	return &OrderCreateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *OrderCreateLogic) OrderCreate(in *order.OrderCreateRequest) (*order.OrderCreateResponse, error) {
	alipay := untils.AliPay{}
	createOrder, err := dao.CreateOrder(in)
	if err != nil {
		pkg.Warn("创建订单失败")
		return nil, err
	}
	return &order.OrderCreateResponse{
		OrderSn: createOrder.OrderSn,
		PayUrl:  alipay.AliPay(createOrder.OrderSn, fmt.Sprintf("%.2f", createOrder.TotalPrice)),
	}, nil
}
